﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BOMS.Utility;

namespace BOMS.WebUI.Web.Warning
{
    public partial class WarningProcess : System.Web.UI.Page
    {
        public int WarningID
        {
            get { return int.Parse(Request.QueryString["warningid"]); }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //得到报警实体
                Model.S_Warning Warn = (new BLL.S_Warning()).GetModel(WarningID);

                //得到与之关联的（电池或电池组）日志记录实体
                BLL.S_BatSubLog bll = new BLL.S_BatSubLog();
                object obj = bll.GetNearlyModel(
                   Warn.WarningTime, Common.GENRATION_NUM, Warn.EquipID, Warn.BatteryGroup, Warn.BatteryID);
               
                string Warningstr;
                if(Warn.BatteryID ==-1)
                {
                    Warningstr = ((GroupErrorStatus)Common.WarningNOLogicalDealwith<GroupErrorStatus>(Warn.WarningNO)).ToString();
                }else{
                    Warningstr = ((BatteryErrorStatus)Common.WarningNOLogicalDealwith<BatteryErrorStatus>(Warn.WarningNO)).ToString();
                }
                
                string WarningInfo;
                object[] parms = new object[]{
                        Warn.WarningTime.ToString("yyyy-MM-dd HH:mm:ss")
                        , Warn.HostName
                        , Warn.BatteryGroup
                        , Warn.BatteryID
                        , Warningstr
                        
                };
                if (Warn.BatteryID != 0 && Warn.BatteryID != -1)//电池
                {
                    WarningInfo = string.Format("<b>{0}</b>现场主机：<b>{1}</b>电池组：<b>{2}</b>电池：<b>{3}</b>异常：<b>{4}</b>", parms);
                    ltrCaption.Text = "电池详情：";

                    Model.S_BatSubLog Bat = obj as Model.S_BatSubLog;
                    if (Bat != null)
                    {
                        string BatInfo = string.Format("电压：<b>{0}V</b>内阻：<b>{1}mΩ</b>温度：<b>{2}℃</b>剩余容量：<b>{3}Ah</b>", Bat.Vval, Bat.Rval, Bat.Tval,Bat.Capacity);
                        ltrBatInfo.Text = BatInfo;
                    }
                }
                else //电池组
                {
                    WarningInfo = string.Format("<b>{0}</b>现场主机：<b>{1}</b>电池组：<b>{2}</b>异常：<b>{4}</b>", parms);
                    ltrCaption.Text = "电池组详情：";

                    Model.S_BatgrpSubLog Grp = obj as Model.S_BatgrpSubLog;
                    if (Grp != null)
                    {
                        //string BatGrpInfo = string.Format("总电压：<b>{0}V</b>总电流：<b>{1}A</b>剩余容量：<b>{2}Ah</b>剩余可用时：<b>{3}</b>工作状态：<b>{4}</b>", Grp.Vval, Grp.Ival, Grp.CapFree,Grp.TimeFree, Grp.Status);
                        string BatGrpInfo = string.Format("总电压：<b>{0}V</b>总电流：<b>{1}A</b>剩余容量：<b>{2}Ah</b>剩余可用时：<b>{3}</b>", Grp.Vval, Grp.Ival, Grp.CapFree, Grp.TimeFree);
                        ltrBatInfo.Text = BatGrpInfo;
                    }
                }
                ltrWarningInfo.Text = WarningInfo;              

                string UserId = HttpContext.Current.User.Identity.Name;
                DataRow dr = GetUserRow(UserId);
                txtOperator.Text = dr["name"].ToString();
                ltrCurDate.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");               

            }
        }
        DataRow GetUserRow(string UserID)
        {
            DataRow dr =BOMS.DAL.SysAdmin.getUserInfoById(UserID).Rows[0];
            return dr;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Model.WarningDealwith model = new Model.WarningDealwith();
            model.CreateTime = DateTime.Now;
            model.CreatorID = int.Parse(HttpContext.Current.User.Identity.Name);
            model.OperateDescr = txtOperateDescr.Value;
            model.Operator = txtOperator.Text;
            model.WarningID = WarningID;

            BLL.WarningDealwith Deal = new BLL.WarningDealwith();
            int nDealwith = Deal.Add(model);
            //Common.GoMsgPage("警报处理情况记录成功！", Model.MsgState.successful, "/Web/Warning/WarningPage.aspx");
            Type cstype = this.GetType();
            ClientScript.RegisterStartupScript(cstype, "key", "dialogReturn('true');", true);
            ClientScript.RegisterStartupScript(cstype, "key", "dialogReturn('false');", true);
        }
    }
}